package mini.expr;

import java.util.HashMap;
import java.util.LinkedList;

import mini.Proc;
import mini.Program;
import mini.compiler.CodeGenerator;
import mini.util.SymbolicNames;

public class Number extends Expr {

	private Integer value;

	public Number(int n){
		super();
		value = new Integer(n);
    }
    
	public Number(Integer n){
		super();
		value = n;
    }
    
	public Integer eval(HashMap hm){
		return value;
    }
	
    /**
     * Routine is from slide 9
     * Load number from constant table value.
     * Store number in temp variable in memory
     */
	public String parse(HashMap<String,Integer> hm, HashMap<String, Proc> functiontable, LinkedList var ) {
		// Store the constants, we'll need those later
		hm.put( getConstantName(), value );
		
    	// Also the temp variable to keep track of it0
    	hm.put(getTempVariable(), null);
		
		// And generator the code
    	CodeGenerator cg = new CodeGenerator();
    	cg.addLoadAccumulator( getConstantName() );
    	cg.addStoreAccumulator( getTempVariable() );    	
		return cg.getCode();
	}
	
	/**
	 * Very simple way to refer to constants.
	 * NOTE: There is code in Proc.java, the isConstant()
	 * method, that relies on the naming convention here.
	 * If you change it, look at the code and adjust
	 * accordingly!
	 * @return
	 */
	public String getConstantName() {
		return SymbolicNames.generateConstantLabel(value);
	}

	@Override
	public String toString() {
		return "Number [value=" + value + "]";
	}
}
